package org.yray.somehttp.sseclient;

import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;

@Slf4j
public class MyEventSourceListener extends EventSourceListener {
    private long curr;

    public MyEventSourceListener (Long curr)
    {
        this.curr=curr;
    }
    private void printEvent(String name) {
        long elapsedNanos = System.currentTimeMillis() - curr;
        log.info("time={},name={}", elapsedNanos, name);
    }

    @Override
    public void onOpen(EventSource eventSource, Response response) {
        printEvent("onOpen");
    }

    @Override
    public void onEvent(EventSource eventSource, String id, String type, String data) {
        printEvent("onEvent");
        System.out.println(data);//请求到的数据
    }

    @Override
    public void onClosed(EventSource eventSource) {
        printEvent("onClosed");
    }

    @Override
    public void onFailure(EventSource eventSource, Throwable t, Response response) {
        printEvent("onFailure");//这边可以监听并重新打开
    }
}
